{% extends 'base/base.html' %}
{% load tags %}

{% block css %}
    <link href="/static/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
{% endblock %}

{% block title %}ITAdmin | 终端--添加用户{% endblock %}


{% block body %}
<form class="form-horizontal" method="post">
    {% csrf_token %}
    <div class="hr-line-dashed"></div>
    <div class="form-group"><label class="col-sm-2 control-label">选择要执行的主机</label>
        <div class="col-sm-10">
            <div class="col-lg-4 m-l-n">
                <select class="form-control" multiple="multiple" size="10" id="server" name="server">
                        {% for minion in minions %}
                            <option value="{{ minion }}">{{ minion }}</option>
                        {% endfor %}
                </select>
            </div>
        </div>
    </div>
    <div class="hr-line-dashed"></div>
    <div class="col-sm-4 col-sm-offset-5">
        <button class="btn btn-outline btn-warning dim" type="button" id="cancel"><i class="fa fa-warning"></i>取消</button>
        <button class="btn btn-primary dim" type="button" id="exec"><i class="fa fa-check"></i>执行</button>
    </div>
</form>
{% endblock %}

{% block base_script %}
    <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script>
{% endblock %}

{% block script %}
<script type="text/javascript">
    $(function () {
        $("#exec").click(function () {
            var minions = new Array();
            for (var i = 0; i < $("#server option:selected").length; i++) {
                var o = $("#server option:selected")[i];
                minions.unshift($(o).attr("value"));
            }
            if (minions.length == 0) {
                swal({title:"错误!", text:"请选择主机", type:"error"});
                return;
            }
            var cmd_id = {{ cmd_id }};
            $.post("{% url 'execsalt' %}", {cmd_id: cmd_id, minions: JSON.stringify(minions)},function (data) {
                if (data.status == 1) {
                    swal({title:"成功!", text:"添加终端用户成功", type:"success"},function () {
                        location.href = "{% url 'saltcmd' %}";
                    });

                } else {
                    swal({title:"错误!", text:data.msg, type:"error"}, function () {
                        location.href = "{% url 'execsalt' %}";
                    });
                }
            }, 'json');
        })
        $('#cancel').click(function () {
            location.href = "{% url 'addterminaluser' %}";
        })
    });
</script>
{% endblock %}